package com.love_weinuo.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.lzy.okgo.callback.StringCallback;
import com.love_weinuo.R;
import com.love_weinuo.bean.MyOrderChangeCallbackBean;
import com.love_weinuo.databinding.ActivityPindanListBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.ui.adapter.IndexFirstAdapter;
import com.love_weinuo.ui.adapter.OnItemClickListener;
import com.love_weinuo.util.GsonUtil;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.SpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/3/18.
 */

public class PindanListActivity extends BaseActivity {

    ActivityPindanListBinding binding;
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> strs = new ArrayList<>();
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> pindanList = new ArrayList<>();
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> waitList = new ArrayList<>();
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> workList = new ArrayList<>();
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> goneList = new ArrayList<>();
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> waitPayList = new ArrayList<>();

    IndexFirstAdapter adapter;
    String titleName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_pindan_list);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        setBack();
        titleName = getIntent().getStringExtra("module_title_name");
        setTitleText(titleName);
        binding.recycler.setLayoutManager(new LinearLayoutManager(PindanListActivity.this));
        adapter = new IndexFirstAdapter(PindanListActivity.this, strs);
        binding.recycler.setAdapter(adapter);
        binding.recycler.setNestedScrollingEnabled(false);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                getPindanDesc(strs.get(position).getId() + "");
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        getAllPindan();
    }

    String userId = (String) SpUtils.get("module_user_id", "");
    public void getAllPindan() {
        HTTPAPI.getInstance().getAllMyOrder(userId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("拼单获取成功" + s);

                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(s);
                    int errorCode = jsonObject.getInt("errcode");
                    if (errorCode == 1) {
                        binding.recycler.setVisibility(View.GONE);
                        binding.ilModuleNothing.setVisibility(View.VISIBLE);
                        showInCommonDialog(jsonObject.getString("msg"));
                        return;
                    }

                    MyOrderChangeCallbackBean bean = GsonUtil.GsonToBean(s, MyOrderChangeCallbackBean.class);
                    pindanList.clear();
                    pindanList.addAll(bean.getResult().getRes());
                    if (pindanList.size() == 0) {
                        if (strs.size() != 0) {
                            showInCommonDialog("暂无更多数据");
                            return;
                        } else {
                            binding.recycler.setVisibility(View.GONE);
                            binding.ilModuleNothing.setVisibility(View.VISIBLE);
                        }
                    } else {
                        binding.recycler.setVisibility(View.VISIBLE);
                        binding.ilModuleNothing.setVisibility(View.GONE);
                        strs.clear();
                        //0 待拼单 1待付定金 2 代付付款 3  待作业 4 已完成
                        for (MyOrderChangeCallbackBean.ResultEntity.ResEntity entity : pindanList) {
                            switch (entity.getOrderstate()) {
                                case "0":
                                    waitList.add(entity);
                                    break;
                                case "2":
                                    waitPayList.add(entity);
                                    break;
                                case "3":
                                    workList.add(entity);
                                    break;
                                case "4":
                                    goneList.add(entity);
                                    break;
                                default:
                            }
                        }
                        switch (titleName) {
                            case "待拼单":
                                strs.addAll(waitList);
                                break;
                            case "待作业":
                                strs.addAll(workList);
                                break;
                            case "已完成":
                                strs.addAll(goneList);
                                break;
                            case "待付款":
                                strs.addAll(waitPayList);
                                break;
                            default:
                        }
                        if (strs.size() == 0) {
                            binding.recycler.setVisibility(View.GONE);
                            binding.ilModuleNothing.setVisibility(View.VISIBLE);
                        }
                        adapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("获取失败" + e.getMessage());
            }
        });
    }

    public void getPindanDesc(String pindanId) {
        HTTPAPI.getInstance().getPidanDesc(userId, pindanId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("拼单详情获取成功" + s);
                startActivity(new Intent(PindanListActivity.this, PindanZxingDescActivity.class)
                        .putExtra("module_result", s));
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("拼单详情获取失败" + e.getMessage());
            }
        });
    }
}
